package com.i2works.smartluolong.manage.utils;

import com.i2works.smartluolong.manage.entity.admin.AdminUserDO;
import com.i2works.smartluolong.utils.base.GlobalConstant;
import com.xiaoleilu.hutool.util.RandomUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 用户登陆信息存储
 *
 * @author Liu Yutao
 * @className BaseModule
 * @email koal@vip.qq.com
 * @date 2016/1/17 14:57
 */

public class BaseModule {

    protected final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 获取登陆用户
     */
    protected AdminUserDO getLoginUser() {
        PrincipalCollection principals = SecurityUtils.getSubject().getPrincipals();
        if (null != principals) {
            return (AdminUserDO) principals.iterator().next();
        }
        return null;
    }

    /**
     * 获取登陆用户ID
     */
    protected Integer getUserId() {
        return getLoginUser().getId();
    }

    /**
     * 获取登陆账号
     */
    protected String getUserName() {
        return getLoginUser().getUserName();
    }


    /**
     * 获取会话ID
     */
    protected String getSessionId() {
        Session session = SecurityUtils.getSubject().getSession();
        if (session != null && session.getId() != null) {
            return String.valueOf(session.getId());
        }
        return null;
    }

    /**
     * 获取验证码KEY
     */
    protected String getVerifyKey() {
        return String.format(GlobalConstant.VERIFY_KEY, getSessionId());
    }

    /**
     * 获取sms验证码KEY
     */
    protected String getSMSKey() {
        return String.format(GlobalConstant.SMS_CODE_KEY, getSessionId());
    }

    /**
     * 获取UUID
     */
    protected String uuid() {
        return RandomUtil.simpleUUID().toUpperCase();
    }
}
